package com.yoomiito.app.widget.wheelview;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by 宋 on 2017/11/19 1:11.
 */

public class Utils {
    public static String[] months = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
    private static final String[] day_28 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
            "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"};
    private static final String[] day_29 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
            "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"};
    private static final String[] day_30 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
            "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"};
    private static final String[] day_31 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
            "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
    private int startYear = 1970;
    private int endYear = getCurrentYear();

    /**
     * 获取目标年月的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static String[] getDaysOfMonth(int year, int month) {
        switch (month) {
            case 4:
            case 6:
            case 9:
            case 11:
                return day_30;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return day_31;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return day_29;
                } else {
                    return day_28;
                }
            default:
                return new String[0];
        }
    }

    public static String[] getYears(int startYear, int endYear) {
        int length = endYear - startYear + 1;
        String[] years = new String[length];
        for (int i = 0; i < length; i++) {
            years[i] = i + startYear + "";
        }
        return years;
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取当前月
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前日
     *
     * @return
     */
    public static int getCurrentDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int getArrayIndex(String[] arry, String value) {
        for (int i = 0; i < arry.length; i++) {
            if (value.equals(arry[i])) {
                return i;
            }
        }
        return -1;
    }

    public static int[] getPositionOfArea(Area area, String province, String city, String distance) {
        int[] positons = new int[3];
        int p_1 = getArrayIndex(area.getProvince(), province);
        if (p_1==-1){
            p_1 = 0;
        }
        positons[0] = p_1;
        Area.Province province1 = area.getProvinces().get(p_1);
        String[] citys = province1.getCitys();
        int p_2 = getArrayIndex(citys, city);
        if (p_2==-1){
            p_2=0;
        }
        positons[1] = p_2;

        int p_3 = getArrayIndex(province1.getCities().get(p_2).getDistracts(),distance);
        if (p_3==-1){
            p_3=0;
        }
        positons[2] = p_3;
        return positons;
    }

    public static Area getAreas(Context context) {
        String str = getFile(context, "area");
        JSONArray ja = null;
        Area area = new Area();
        try {
            ja = new JSONArray(str);
            String[] provinces = new String[ja.length()];
            area.setProvince(provinces);
            List<Area.Province> provinceList = new ArrayList<>();
            area.setProvinces(provinceList);
            for (int i = 0; i < ja.length(); i++) {
                JSONObject jb = ja.getJSONObject(i);
                provinces[i] = jb.getString("name");

                JSONArray ja2 = jb.getJSONArray("city");
                Area.Province province = new Area.Province();
                String[] citys = new String[ja2.length()];
                province.setCitys(citys);
                List<Area.City> cityList = new ArrayList<>();
                province.setCities(cityList);
                provinceList.add(province);
                for (int j = 0; j < ja2.length(); j++) {
                    JSONObject jb2 = ja2.getJSONObject(j);
                    citys[j] = jb2.getString("name");

                    JSONArray ja3 = jb2.getJSONArray("area");
                    Area.City city = new Area.City();
                    String[] distracts = new String[ja3.length()];
                    city.setDistracts(distracts);
                    cityList.add(city);
                    for (int k = 0; k < ja3.length(); k++) {
                        distracts[k] = ja3.getString(k);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return area;
    }

    public static String getFile(Context context, String fileName) {
        StringBuilder sb = new StringBuilder();
        AssetManager am = context.getAssets();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    am.open(fileName)));
            String next = "";
            while (null != (next = br.readLine())) {
                sb.append(next);
            }
        } catch (IOException e) {
            e.printStackTrace();
            sb.delete(0, sb.length());
        }
        return sb.toString();
    }
}
